﻿using System.Reflection;
using System.Reflection.Extension;
using UnityEngine;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace System.Extension
{
    public static class stringExtension
    { 
        public static string[] GetComponents(this string source, char splitBy = '/')
        {
            return source.isNullOrEmpty() ? null : source.Split(splitBy);
        }

        public static int GetCurrency(this string source)
        {
            string currencyStr = source.Remove(source.Length - 1);
            return Convert.ToInt32(currencyStr); ;
        }

       public static bool isNotEmpty(this string source)
        {
           return !string.IsNullOrEmpty(source);
        }

       public static bool isNullOrEmpty(this string source)
       {
           return string.IsNullOrEmpty(source);
       }

       public static bool FileExists(this string path)
       {
           return File.Exists(path);
       }
       public static bool isResourceExists(this string path)
       {
           return path.isNotEmpty() && path.FileExists();
       }

       public static int ToInt(this string source)
        {
            int result = 0;
            if (source.isNotEmpty())
                result = int.Parse(source);
            return result;
        }

        public static float ToFloat(this string source)
        {
            float result = 0;
            if (source.isNotEmpty())
                result = float.Parse(source);
            return result;
        }

        public static double ToSeconds(this string time, string format = "HH:mm:ss")
       {
           //Debug.Log("Time :" + time);
           DateTime dt = DateTime.ParseExact(time, format, System.Globalization.CultureInfo.InvariantCulture);
           return dt.TimeOfDay.TotalSeconds;
       }
       public static string ToTime(this double secs)
       {
           TimeSpan t = TimeSpan.FromSeconds(secs);
           string answer = string.Format("{0:D2}:{1:D2}:{2:D2}", t.Hours, t.Minutes,  t.Seconds);
           return answer;
       }
       public static string ToNumberWord(this int source)
       {
           Dictionary<int, string> words= new Dictionary<int, string>() {{1, "first"}, {2, "second"}, {3,"third"}, {4,"fourth"},
           {5, "fifth"}, {6, "sixth"}, {7, "seventh"}, {8, "eighth"}, {9,"ninth"}, {10,"ninth"}};
           return words[source];
       }
        public static string ToTier(this int source)
        {
            Dictionary<int, string> words = new Dictionary<int, string>() {{0, "UW I"}, {1, "UW II"}, {2,"WRB II"}, {3,"WRB II"}};
            return words[source];
        }
        /*
        public static string GetStringBetween(this string source, char start, char end)
        {
            string format = @"\" + start + "(\\w+)\\" + end ;
            return Regex.Match(source,format).Groups[1].Value;
            /*int istart = source.IndexOf(start) + 1;
            int iend = source.IndexOf(end, start);
            return source.Substring(istart, iend - istart);
        }*/

        public static string GetStringBetween(this string value, string a, string b)
        {
            int posA = value.IndexOf(a);
            int posB = value.LastIndexOf(b);
            if (posA == -1)
            {
                return "";
            }
            if (posB == -1)
            {
                return "";
            }
            int adjustedPosA = posA + a.Length;
            if (adjustedPosA >= posB)
            {
                return "";
            }
            return value.Substring(adjustedPosA, posB - adjustedPosA);
        }

        public static string ToPlural(this string source)
        {
            string result = string.Empty;
            if(source.isNotEmpty())
            {
                bool iesCondition = source.EndsWith("y", true, null);
                result = iesCondition ? (source.Remove(source.Length - 1, 1) +  "ies" ): (source + "s");
            }
            return result;
        }

        public static int GetDigit(this int source, int pos)
        {
            int result = 0;
            int pow = (int)Mathf.Pow(10, pos);
            result = source / pow;
            return result;
        }
    }
}